/**

 * 

 */

package ca.sfu.util;



import edu.cmu.tetrad.data.CovarianceMatrix;

import edu.cmu.tetrad.data.RectangularDataSet;

import edu.cmu.tetrad.graph.Graph;

import edu.cmu.tetrad.graph.GraphUtils;

import edu.cmu.tetrad.search.GesSearch;

import edu.cmu.tetradapp.model.DataWrapper;

import edu.cmu.tetradapp.model.GesIndTestParams;

import edu.cmu.tetradapp.model.GesParams;

import edu.cmu.tetradapp.model.GesRunner;



import java.util.*;



/**

 * @author wluoa

 *

 */

public class GesMultipleRunner extends GesRunner {



	private int numGraphs=2; // Temporary code, to be removed later

	/**

	 * @param dataWrapper

	 * @param params

	 */

	public GesMultipleRunner(DataWrapper dataWrapper, GesParams params) {

		super(dataWrapper, params);

		// TODO Auto-generated constructor stub

	}



	/**

	 * @param args

	 */

	public static void main(String[] args) {

		// TODO Auto-generated method stub



	}

	

	public void execute(){

    GesMultipleSearch gesMultipleSearch;

    Object source = getDataModel();



    if (source instanceof CovarianceMatrix) {

        gesMultipleSearch = new GesMultipleSearch((CovarianceMatrix) source);

        gesMultipleSearch.setKnowledge(getParams().getKnowledge());

        gesMultipleSearch.setSamplePrior(((GesParams) getParams()).getCellPrior());

        gesMultipleSearch.setStructurePrior(

                ((GesParams) getParams()).getStructurePrior());

    }

    else if (source instanceof RectangularDataSet) {

        gesMultipleSearch = new GesMultipleSearch((RectangularDataSet) source);

        gesMultipleSearch.setKnowledge(getParams().getKnowledge());

        gesMultipleSearch.setSamplePrior(((GesParams) getParams()).getCellPrior());

        gesMultipleSearch.setStructurePrior(

                ((GesParams) getParams()).getStructurePrior());

    }

    else {

        throw new RuntimeException(

                "GES does not accept this type of data set.");

    }



    GesIndTestParams gesIndTestParams =

            (GesIndTestParams) getParams().getIndTestParams();

    gesMultipleSearch.setMinimalImprovement(

            gesIndTestParams.getMinimalImprovement());



    Queue<Graph> searchGraphs = gesMultipleSearch.multipleSearch();

    Graph[] graphs = null;

    searchGraphs.toArray(graphs);

    setResultGraph(graphs[graphs.length]);

    GraphUtils.arrangeBySourceGraph(getResultGraph(), getSourceGraph());}



}

